Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

101
Views
JavaScript document.createElement("a") problema con un atributo.href

¿Es normal este comportamiento al crear un enlace con document.createElement("a") ?

 function updateUl(newVal){ let a = document.createElement("a") let newListItem = document.createElement("li") a.appendChild(document.createTextNode(newVal)) console.log(newVal) //output: "www.google.com" a.href = newVal console.log(a.href) //output: "http://127.0.0.1:5500/www.google.com" a.target = "_blank" newListItem.appendChild(a) ulEl.appendChild(newListItem) }

Luego, claramente, cuando hago clic en el enlace, aparece un error de página no encontrada .

¿Que esta pasando?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Sin un protocolo, se supone que es una ruta en la página actual. Agregue el protocolo https:// para que navegue.

 function updateUl(newVal){ // pass in as "https://www.google.com" let a = document.createElement("a") let newListItem = document.createElement("li") a.appendChild(document.createTextNode(newVal)) console.log(newVal) //output: "https://www.google.com" a.href = newVal console.log(a.href) //output: "https://www.google.com" a.target = "_blank" newListItem.appendChild(a) ulEl.appendChild(newListItem) }

o agregarlo dinámicamente

 function updateUl(newVal){ if (!newVal.startsWith("http://") && !newVal.startsWith("https://") { newVal = `https://{newVal}`; } let a = document.createElement("a") let newListItem = document.createElement("li") a.appendChild(document.createTextNode(newVal)) console.log(newVal) //output: "https://www.google.com" a.href = newVal console.log(a.href) //output: "https://www.google.com" a.target = "_blank" newListItem.appendChild(a) ulEl.appendChild(newListItem) }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!